﻿using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.UI;

namespace Gp.Scripts.GUI {
    public class HealthBarUI : MonoBehaviour {
        [LabelText("显示")] public Image display;
        [LabelText("缓冲")] public Image buffer;
        

        private float _targetRate; 
        public float bufferSpeed = 50f;

        private Vector3 _orgScale;
        private float _value;

        private void Start() {
            _orgScale = display.transform.localScale;
        }


        private void Update() {
            float p = Mathf.Lerp(display.transform.localScale.x, _targetRate, bufferSpeed * Time.deltaTime);
            var temp = _orgScale;
            temp.x *= p; 
            display.transform.localScale = temp;
        }
        
        
        
        public void SetValue(int value, int max) {
            _value = value;
            _targetRate = max == 0 ? 0 : value / (float) max;
        }


        public void Blink(int damage) {
            buffer.fillAmount = damage / _value;
        }

        public void UnBlink() {
            buffer.fillAmount = 0;
        }
    }
}